package com.leesche.zcsp.node.web.controller.web.v2.combo;

import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.controller.web.v2.admin.WebAdminController;
import com.leesche.zcsp.node.web.model.PubDiscount;
import com.leesche.zcsp.node.web.service.PubDiscountService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDiscountDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDiscountCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by kaider on 2016/12/2.
 */
@Controller
@RequestMapping("/v2/combo")
public class WebComboController extends WebAdminController {

    private final static Logger LOGGER = LoggerFactory.getLogger(WebComboController.class);

    private static final String SHOW_VIEW = "web/v2/combo/showView";

    @Autowired
    private PubDiscountService discountService;

    /**
     * 套餐列表
     * @param condition
     * @param model
     * @return
     */
    @RequestMapping(value = "/showView", method = RequestMethod.GET)
    public String todayView(
            QueryPubDiscountCondition condition,
            Model model
    ){
        getComboList(condition,model);
        model.addAttribute("view","1");
        return SHOW_VIEW;
    }

    /**
     * 修改套餐币数
     * @param discountId
     * @param comboCoin
     * @return
     */
    /*@ResponseBody
    @RequestMapping(value = "/editCoin",method = RequestMethod.POST)
    public Map<String, Object> editComboCoin(
            Long discountId,
            Integer comboCoin
    ){
        try{
            PubDiscount discount = discountService.selectByPrimaryKey(discountId);
            discount.setCoin(comboCoin);
            discount.setDiscountname(discount.getMoney()+"元"+comboCoin+"币");
            discountService.updateByPrimaryKeySelective(discount);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }*/

    @ResponseBody
    @RequestMapping(value = "/editCoin",method = RequestMethod.POST)
    public Map<String, Object> editComboCoin(
            Long discountId,
            Integer comboCoin,
            String discountName
    ){
        try{
            PubDiscount discount = discountService.selectByPrimaryKey(discountId);
            discount.setCoin(comboCoin);
            discount.setDiscountname(discountName);
            discountService.updateByPrimaryKeySelective(discount);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 批量操作页面
     * @param condition
     * @param model
     * @return
     */
    @RequestMapping(value = "/editBatchView",method = RequestMethod.GET)
    public String editBatchView(
            QueryPubDiscountCondition condition,
            Model model
    ){
        getComboList(condition,model);
        model.addAttribute("view","2");
        return SHOW_VIEW;
    }

    /**
     * 批量修改操作
     * @param discounts
     * @param coins
     * @param money
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editBatch",method = RequestMethod.POST)
    public Map<String, Object> editBatch(
            Long[] discounts,
            Integer[] coins,
            Integer[] money,
            String[] discountName
    ){
        try{
            for(int i=0;i<discounts.length;i++){
                PubDiscount discount = discountService.selectByPrimaryKey(discounts[i]);
                discount.setCoin(coins[i]);
//                discount.setMoney(money[i]);
//                discount.setDiscountname(discountName[i]);
                discountService.updateByPrimaryKeySelective(discount);
            }
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 批量删除
     * @param discounts
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editBatchDel",method = RequestMethod.POST)
    public Map<String, Object> editBatchDel(
            Long[] discounts
    ){
        try{
            for(Long discountId : discounts){
                PubDiscount discount = discountService.selectByPrimaryKey(discountId);
                if(discount==null){
                    return WebResult.failure("套餐："+discount.getDiscountname()+"，已经被设备使用，不能删除").toMap();
                }
                discountService.deleteByPrimaryKey(discountId);
            }
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 添加界面
     * @param model
     * @return
     */
    @RequestMapping(value = "/addView",method = RequestMethod.GET)
    public String addView(
            Model model
    ){
        model.addAttribute("view","3");
        return SHOW_VIEW;
    }

    /**
     * 添加操作
     * @param money
     * @param coin
     * @return
     */
    /*@ResponseBody
    @RequestMapping(value = "/addCombo",method = RequestMethod.POST)
    public Map<String, Object> addCombo(
            Integer money,
            Integer coin
    ){
        try {
            Long adminId = getAdminId();
            PubDiscount discount = new PubDiscount();
            discount.setDiscountname(money+"元"+coin+"币");
            discount.setMoney(money);
            discount.setCoin(coin);
            discount.setAdminid(adminId);
            discountService.insert(discount);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }*/

    @ResponseBody
    @RequestMapping(value = "/addCombo",method = RequestMethod.POST)
    public Map<String, Object> addCombo(
            Integer money,
            Integer coin,
            String discountName
    ){
        try {
            Long adminId = getAdminId();
            PubDiscount discount = new PubDiscount();
            discount.setDiscountname(discountName);
            discount.setMoney(money);
            discount.setCoin(coin);
            discount.setAdminid(adminId);
            discountService.insert(discount);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 获取用户套餐列表
     * @param condition
     * @param model
     * @return
     */
    private Model getComboList(
        QueryPubDiscountCondition condition,
        Model model
    ){
        Long adminId = getAdminId();
        condition.setAdminId(adminId);
        List<PubDiscountDto> discountDtos = discountService.findDiscountList(condition);
        model.addAttribute("comboList",discountDtos);
        return model;
    }

}
